﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PMS.Logic.BD
{
    public partial class GasAjax : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String methodName = Request["method"];
            Type type = this.GetType();
            MethodInfo method = type.GetMethod(methodName);
            if (method == null) throw new Exception("method is null");
            try
            {
                method.Invoke(this, null);
            }
            catch
            {
                throw;
            }
        }
        /// <summary>
        /// 获取气体信息
        /// </summary>
        public void GetGasList()
        {
            List<SearchCondition> listSearch = new List<SearchCondition>();
            if (Request.Form["searchData"] != null)
            {
                string searchData = Request.Form["searchData"];
                listSearch = ModelTools.GetSearchContions(searchData);
            }
            Expression<Func<View_BD_GasInfo, bool>> exp = SearchExpression.FindByGroup<View_BD_GasInfo>(listSearch);
            string OrderId = OperateContext.Sort == null ? "ID" : OperateContext.Sort;
            int count = 0;
            var list = DALSession.Current.View_BD_GasInfoDAL.GetPagedList(OperateContext.Page, OperateContext.Rows, exp, OrderId, OperateContext.Order);
            count = DALSession.Current.View_BD_GasInfoDAL.GetCount(exp);
            Response.Write(JsonHelper.toPageJson(list, count));
        }


        /// <summary>
        /// 根据q获取气体信息
        /// </summary>
        public void GetClassGasInfoByQ()
        {
            string q = Request.Form["q"];
            int intQ = Commons.String2Int32(q);
            var a = DALSession.Current.View_BD_UserDepInfoDAL.GetPagedList(OperateContext.Page, OperateContext.Rows, m => (m.ID == intQ || m.UserName.Contains(q)  || m.DepName.Contains(q)) , "ID", OperateContext.OrderDesc).Select(m => new { value = m.ID, text = m.UserName, DepName = m.DepName, DepId = m.DepId,UserState = m.UserState });
            int count = DALSession.Current.View_BD_UserDepInfoDAL.GetCount(m => (m.ID == intQ || m.UserName.Contains(q) || m.DepName.Contains(q)) );
            Response.Write(JsonHelper.toPageJson(a, count));
            //&& m.FatherId == 4,, && m.FatherId == 4
        }

        /// <summary>
        /// 获取气体ID,Name
        /// </summary>
        public void GetAllGasList() {
            Response.Write(JsonHelper.toJson(DALSession.Current.BD_GasInfoDAL.GetListBy().Select(p => new { value = p.ID, text = p.GasChiName })));       
        }

        /// <summary>
        /// 修改气体信息
        /// </summary>
        public void EditGas()
        {
            int ID = Commons.String2Int32(Request.Form["ID"]);
            BD_GasInfo perNew = DALSession.Current.BD_GasInfoDAL.GetListBy(d => d.ID == ID).FirstOrDefault();
            ModelTools.Form2EditModel(Request.Form, perNew);
            checkLel(perNew);
            DALSession.Current.BD_GasInfoDAL.ModifyAllModel(perNew);
            DALSession.Current.BD_GasInfoDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true, "保存成功"));
        }
        /// <summary>
        /// 添加气体信息
        /// </summary>
        public void AddGas()
        {
            BD_GasInfo per = new BD_GasInfo();
            ModelTools.Form2EditModel(Request.Form, per);
            checkLel(per);
            DALSession.Current.BD_GasInfoDAL.Add(per);
            DALSession.Current.BD_GasInfoDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true, "保存成功"));
        }
        /// <summary>
        /// 删除气体信息
        /// </summary>
        public void DelGas()
        {
            //  int Id = Commons.String2Int32(Request.Form["Id"]);
            int Id = Convert.ToInt32(Request.Form["Id"].ToString());
            DALSession.Current.BD_GasInfoDAL.DelBy(p => p.ID == Id);
            DALSession.Current.BD_GasInfoDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true, "删除成功"));
        }

        public void checkLel(BD_GasInfo gas) {
            if (null != gas.Lel)
            {
                double res;
                if (double.TryParse(gas.Lel.ToString(), out res))
                {
                    gas.Lel = res/100;
                }
            }
            else {
                gas.Lel = 0;
            }
        }
    }
}